\section{Controller}
\label{sec:Controller}
In this section, the design and implementation of the real-time controller controlling the JIWY setup is discussed. The controller interacts with the JIWY using encoder inputs and PWM outputs as described in section \ref{sec:vhdl}. For the controller design, Model Driven Design is used using the 20-sim modeling and simulation tool. Here, the controller implementation follows the controller design by using code generation.

\subsection{PID controller}
As a starting point for the controller a 20-sim testbench of the JIWY setup has been made available, see figure~\ref{fig:model_overview}. In this model, position controlling for the horizontal and vertical movements is implemented using two PID controllers.

\begin{figure}[h!]
	\includegraphics[scale=0.6]{images/model_overview.png} 
	\caption{Overview of JIWY testbench}
	\label{fig:model_overview}
\end{figure}

The PID controllers are simulated to verify the dynamic behaviour, the results for the horizontal and vertical controllers are given in figure~\ref{fig:JIWY_horizontal_control} and \ref{fig:JIWY_vertical_control} respectively. In the figures, it can be seen that the horizontal control results in overshoot with a corresponding settling time and that the vertical control is quick. The vertical PID settings are kept as they are given, but the horizontal PID settings are changed in order to achieve a faster performance. The result is given in figure~\ref{fig:JIWY_horizontal_control} too.

\begin{figure}[h!]
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[scale=0.6]{images/JIWY_horizontal_control.png}
\caption{JIWY horizontal control simulation}
\label{fig:JIWY_horizontal_control}
\end{minipage}%
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[scale=0.6]{images/JIWY_vertical_control.png}
\caption{JIWY vertical control simulation}
\label{fig:JIWY_vertical_control}
\end{minipage}
\end{figure}


\subsection{Homing}
The horizontal and vertical movement of the JIWY setup is limited due to mechanical end-stops, a mechanism that ensures that the wiring of the setup remains intact. In this subsection a homing procedure is designed that detects the end-stop positions.

For the homing algorithm a simple strategy is being used in which the motors are controlled using a fixed PWM signal during a fixed time period. Thereafter, the count is being stored as it is assumed that an endstop has been reached. An iconic diagram of the homing controller is given in figure~\ref{fig:homing_controller}. Here, depending on the statemachine, a control signal is send which is either zero, positive constant or negative constant valued.

\begin{figure}[h!]
	\centering
	\includegraphics[scale=0.6]{images/homing_controller.png} 
	\caption{Overview of JIWY testbench}
	\label{fig:homing_controller}
\end{figure}

The internals of the state machine are given in figure~\ref{fig:homing_statemachine}. In the first state, the value of the constant source block is transferred to the controlSignal output and in the second state, the negative constant value is set as output.

\subsection{Integration}

\begin{figure}[h!]
\begin{minipage}{0.5\linewidth}
	\centering
	\includegraphics[scale=0.6, clip=true, trim=4cm 0 0 0]{images/homingStatemachine.png} 
	\caption{Overview of the homing statemachine}
	\label{fig:homing_statemachine}
\end{minipage}%
\begin{minipage}{0.5\linewidth}
\centering
	\includegraphics[scale=0.6, clip=true, trim=4cm 0 0 0]{images/controllerStateMachine.png} 
	\caption{Statemachine interconnecting the Horizontal and Vertical controller}
	\label{fig:controller_statemachine}
\end{minipage}
\end{figure}

The PositionControllerPan and PositionControllerTilt as given in figure~\ref{fig:model_overview} are both extended with the homing procedure and are interconnected using a state machine that handles the homing and normal behaviour. See figure~\ref{fig:controller_interconnects} and \ref{fig:controller_statemachine} for the state machine interconnection and state diagram.

\begin{figure}[h!]
\centering
	\includegraphics[scale=0.6]{images/controller.png} 
	\caption{State machine interconnecting Horizontal and Vertical controller}
	\label{fig:controller_interconnects}
\end{figure}

The top-level state machine respectively handles the horizontal and vertical homing. Then, the controller enters a waiting state, in which nothing is done for 1 seconds. This timeslot could have been used to steer the JIWY to some initial position, but that is not implemented yet. Finally, the system enters the state of normal PID control in which the PID controllers control the setup according to the reference signals. 

The internals of the horizontal and vertical controller are given in \ref{fig:horizontal_controller}. In addition to the components discussed, a reference controller and PWM driver are given. The reference controller filters the reference input signal using a moving average filter\footnote{The moving average filter is created using four delay elements and taking the average of the current value and the delayed values} and biases the input such that a zero reference input corresponds to halfway the end-stops. Furthermore, the reference input is limited to the minimum and maximum possible positions (i.e. the end-stops found), in order to limit the motor currents whenever a setpoint has been given that cannot be reached. The PWM driver scales the control output to a signed 10 bits signal such that the model output can be directly fed to the PWM.

\begin{landscape}
\begin{figure}[h!]
\centering
	\includegraphics[scale=0.6]{images/horizontal_controller.png} 
	\caption{Horizontal controller}
	\label{fig:horizontal_controller}
\end{figure}
\end{landscape}



\subsection{Testing}
The \texttt{MotorPan} and \texttt{MotorTilt} plant models are extended with an X1-junction that effectively switches an additional spring and resistance after one radian. Furthermore, the initial condition of the plant is made 0.4 and -0.3 radians for the horizontal and vertical movements. The simulation results for the plant position, PWM output and setpoint are given in figure~\ref{fig:Controller_Simulation} for both the horizontal and vertical movements.

\begin{figure}[h!]
\centering
	\includegraphics[scale=0.6]{images/controller_simulation_results.png} 
	\caption{Simulation results for horizontal and vertical movements}
	\label{fig:Controller_Simulation}
\end{figure}

In figure~\ref{fig:Controller_Simulation} it can be seen that the horizontal and vertical are handled first sequentially. Then, the controller is idle for some time (5 seconds). In the real setup, the camera will return to its preferred position due to gravitation, however, gravitation is not implemented in the model. 

After 22 seconds, the PID controllers are tuned in and they nicely follow the input sine signal. 

Furthermore, the effects of the reference controller can be seen. The sine signal is effectively middled between the two end-stops due to the biasing and whenever the plant reaches its end-stops, the PWM signal remains low instead of clipping to the limits of the saturation block. Note that the magnitude of the PWM signal once hitting an end-stop is dictated by the integrator in the PID controller.
